package com.wzt.lianfirecontrol.fragment.yuyueweibao;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.activity.ShowFragActivity;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.BannerModel;
import com.wzt.lianfirecontrol.bean.recode.address.BuildingModel;
import com.wzt.lianfirecontrol.bean.recode.address.FloorModel;
import com.wzt.lianfirecontrol.bean.recode.device.DeviceModel;
import com.wzt.lianfirecontrol.bean.recode.function.yuyueweibao.WeiBaoCompanyModel;
import com.wzt.lianfirecontrol.bean.recode.user.UserInfoModel;
import com.wzt.lianfirecontrol.fragment.BaseFragment;
import com.wzt.lianfirecontrol.http.HttpHelper;
import com.wzt.lianfirecontrol.http.json.ParseJsonData;
import com.wzt.lianfirecontrol.http.Url;
import com.wzt.lianfirecontrol.utils.DialogUtils;
import com.wzt.lianfirecontrol.utils.GildeUtils;
import com.wzt.lianfirecontrol.utils.JSONUtil;
import com.wzt.lianfirecontrol.utils.MinioUtils;
import com.wzt.lianfirecontrol.utils.PreferencesUtils;
import com.wzt.lianfirecontrol.utils.ScreenUtils;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.ToastUtils;
import com.wzt.lianfirecontrol.utils.Utils;
import com.wzt.lianfirecontrol.view.InitView;

import org.json.JSONArray;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class AddYuYueFragment extends BaseFragment {
    private BaseActivity activity;
    private Bundle bundle;
    private View itemContentView;

    /**
     * 设备类型
     */
    private LinearLayout ll_title;
    private TextView tv_title;
    private TextView tv_status;
    /**
     * 选择楼栋楼层
     */
    private LinearLayout ll_select_building;
    private TextView tv_select_building;
    private View iv_select_building;
    /**
     * 选择设备
     */
    private EditText et_device_name;
    private ImageView iv_add_device_name;
    /**
     * 设备位置
     */
    private EditText et_deice_location;
    private ImageView iv_clear_device_location;
    /**
     * 选择楼栋楼层
     */
    private LinearLayout ll_weibao_company;
    private TextView tv_weibao_company;
    /**
     * 问题描述
     */
    private EditText et_error_content;
    private ImageView iv_clear_error_content;
    /**
     * 上传或者显示图片
     */
    private View include_play_photo_three;
    private RelativeLayout rl_device_photo_1;
    private ImageView iv_device_photo_1;
    private ImageView iv_photo_delete_1;
    private RelativeLayout rl_device_photo_2;
    private ImageView iv_device_photo_2;
    private ImageView iv_photo_delete_2;
    private RelativeLayout rl_device_photo_3;
    private ImageView iv_device_photo_3;
    private ImageView iv_photo_delete_3;
    /**
     * 提交按钮
     */
    private Button bt_submit;

    private View include_progress_bar;

    private static final int SUBMIT_DETAIL_WHAT = 0;
    private static final int FIND_BUILDING_WHAT = 3;
    private static final int FINA_COMPANY_WHAT = 4;

    private String[] imgUrl = new String[]{"", "", ""};
    private boolean isUpdateImg = false;
    private boolean isSubmitSuccess = false;

    private Dialog chooseBuildingDialoag;
    private Dialog chooseFloorDialoag;
    private Dialog chooseDeviceDialoag;

    private int currentSelectBuilding = -1;
    private int currentSelectFloor = -1;
    private int currentSelectDevice = -1;

    private List<BuildingModel> buildingModels = new LinkedList<>();
    private List<WeiBaoCompanyModel> weiBaoCompanyModels = new LinkedList<>();
    private int currentSelectCompany = -1;
    private Dialog chooseCompanyDialog;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == itemContentView){
            bundle = getArguments();
            activity = getMyActivity();
            itemContentView = inflater.inflate(R.layout.f_add_yuyue, container, false);
            initView();
        }
        if (itemContentView.getParent() != null) {
            ((ViewGroup) itemContentView.getParent()).removeView(itemContentView);
        }
        return itemContentView;
    }

    private void initView() {
        ll_title = itemContentView.findViewById(R.id.ll_title);
        tv_title = itemContentView.findViewById(R.id.tv_title);
        tv_status = itemContentView.findViewById(R.id.tv_status);
        ll_title.setVisibility(View.GONE);

        ll_select_building = itemContentView.findViewById(R.id.ll_select_building);
        tv_select_building = itemContentView.findViewById(R.id.tv_select_building);
        iv_select_building = itemContentView.findViewById(R.id.iv_select_building);
        ll_select_building.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentSelectBuilding = -1;
                currentSelectFloor = -1;
                currentSelectDevice = -1;
                tv_select_building.setText("");
                // 选择楼栋楼层
                initFindBuildingHttpHelper();
            }
        });

        et_device_name = itemContentView.findViewById(R.id.et_device_name);
        et_device_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_device_name.setText("");
                currentSelectDevice = -1;
            }
        });

        iv_add_device_name = itemContentView.findViewById(R.id.iv_add_device_name);
        iv_add_device_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (buildingModels.size() == 0 || currentSelectBuilding < 0 || currentSelectFloor < 0){
                    ToastUtils.showToast(activity, "请先选择楼栋楼层");
                    return;
                }
                List<DeviceModel> deviceModels = buildingModels.get(currentSelectBuilding)
                        .getChildren().get(currentSelectFloor).getChildren();

                if (null != deviceModels && deviceModels.size() > 0) {
                    List<BannerModel> bannerModels = new ArrayList<>();
                    for (DeviceModel deviceModel:deviceModels){
                        BannerModel bannerModel = new BannerModel();
                        bannerModel.setTitle(deviceModel.getName());
                        bannerModel.setViceTitle(deviceModel.getAddress());
                        bannerModels.add(bannerModel);
                    }
                    chooseDeviceDialoag = DialogUtils.getChooseListDialog(activity, bannerModels, "选择设备", currentSelectDevice, new BaseRecyclerAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(View v, int position, Object object) {
                            currentSelectDevice = position;
                            if (object instanceof BannerModel){
                                BannerModel bannerModel = (BannerModel)object;
                                et_device_name.setText(bannerModel.getTitle());
                                et_deice_location.setText(bannerModel.getViceTitle());
                            }
                            DialogUtils.closeDialog(chooseDeviceDialoag);

                        }
                    });
                    chooseDeviceDialoag.show();
                }else {
                    ToastUtils.showToast(activity, "暂无设备信息");
                }
            }
        });

        ll_weibao_company = itemContentView.findViewById(R.id.ll_weibao_company);
        tv_weibao_company = itemContentView.findViewById(R.id.tv_weibao_company);
        ll_weibao_company.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentSelectCompany = -1;
                weiBaoCompanyModels.clear();
                tv_weibao_company.setText("");
                // 选择维保单位
                initFindCompanyHttpHelper();
            }
        });

        et_deice_location = itemContentView.findViewById(R.id.et_deice_location);
        iv_clear_device_location = itemContentView.findViewById(R.id.iv_clear_device_location);
        et_deice_location.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (StringUtils.isEmpty(et_deice_location.getText().toString())){
                    iv_clear_device_location.setVisibility(View.GONE);
                }else {
                    iv_clear_device_location.setVisibility(View.VISIBLE);
                }
            }
        });
        iv_clear_device_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_deice_location.setText("");
            }
        });


        et_error_content = itemContentView.findViewById(R.id.et_error_content);
        iv_clear_error_content = itemContentView.findViewById(R.id.iv_clear_error_content);
        et_error_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (StringUtils.isEmpty(et_error_content.getText().toString())){
                    iv_clear_error_content.setVisibility(View.GONE);
                }else {
                    iv_clear_error_content.setVisibility(View.VISIBLE);
                }
            }
        });
        iv_clear_error_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_error_content.setText("");
            }
        });

        initPhotoThree();

        bt_submit = itemContentView.findViewById(R.id.bt_submit);
        bt_submit.setVisibility(View.VISIBLE);
        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 提交
                initAddDetailHttpHelper();
            }
        });



        include_progress_bar = itemContentView.findViewById(R.id.include_progress_bar);
        InitView.instance().initProgressBar(activity, include_progress_bar);
    }

    private void initPhotoThree() {
        include_play_photo_three = itemContentView.findViewById(R.id.include_play_photo_three);
        rl_device_photo_1 = itemContentView.findViewById(R.id.rl_device_photo_1);
        iv_device_photo_1 = itemContentView.findViewById(R.id.iv_device_photo_1);
        iv_photo_delete_1 = itemContentView.findViewById(R.id.iv_photo_delete_1);
        rl_device_photo_2 = itemContentView.findViewById(R.id.rl_device_photo_2);
        iv_device_photo_2 = itemContentView.findViewById(R.id.iv_device_photo_2);
        iv_photo_delete_2 = itemContentView.findViewById(R.id.iv_photo_delete_2);
        rl_device_photo_3 = itemContentView.findViewById(R.id.rl_device_photo_3);
        iv_device_photo_3 = itemContentView.findViewById(R.id.iv_device_photo_3);
        iv_photo_delete_3 = itemContentView.findViewById(R.id.iv_photo_delete_3);

        int width = (ScreenUtils.getScreenWidth(activity) - Utils.dip2px(activity, 90)) / 3;
        rl_device_photo_1.getLayoutParams().width = width;
        rl_device_photo_1.getLayoutParams().height = width;
        rl_device_photo_2.getLayoutParams().width = width;
        rl_device_photo_2.getLayoutParams().height = width;
        rl_device_photo_3.getLayoutParams().width = width;
        rl_device_photo_3.getLayoutParams().height = width;

        iv_device_photo_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg){
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                if (StringUtils.isEmpty(imgUrl[0])){
                    addImageOrPlayPhoto(ConstData.PHOTO_DEVICE_LIST_1, false);
                }else {
                    ((ShowFragActivity)activity).setShowImg(imgUrl[0]);
                }
            }
        });
        iv_photo_delete_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg){
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                MinioUtils.removeFile(activity, imgUrl[0], handler);
                imgUrl[0] = "";
                updateImageView();
            }
        });

        iv_device_photo_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg){
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                if (StringUtils.isEmpty(imgUrl[1])){
                    addImageOrPlayPhoto(ConstData.PHOTO_DEVICE_LIST_2, false);
                }else {
                    ((ShowFragActivity)activity).setShowImg(imgUrl[1]);
                }
            }
        });
        iv_photo_delete_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg){
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                MinioUtils.removeFile(activity, imgUrl[1], handler);
                imgUrl[1] = "";
                updateImageView();
            }
        });

        iv_device_photo_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg){
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                if (StringUtils.isEmpty(imgUrl[2])){
                    addImageOrPlayPhoto(ConstData.PHOTO_DEVICE_LIST_3, false);
                }else {
                    ((ShowFragActivity)activity).setShowImg(imgUrl[2]);
                }
            }
        });
        iv_photo_delete_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg){
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                MinioUtils.removeFile(activity, imgUrl[2], handler);
                imgUrl[2] = "";
                updateImageView();
            }
        });
        updateImageView();
    }

    /**
     * 查找楼栋
     */
    private void initFindBuildingHttpHelper() {
        if (!Utils.hasNetwork(activity)){
            ToastUtils.showToast(activity, "网络未连接");
            return;
        }
        if (isUpdateImg){
            ToastUtils.showToast(activity, "正在上传图片");
            return;
        }
        include_progress_bar.setVisibility(View.VISIBLE);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id", UserInfoModel.getCompandId(activity));

        HashMap<String, String> url_params = new HashMap<>();
        url_params.put(ConstData.JSONOBJECT, JSONUtil.mapToJSonStr(hashMap));
        HttpHelper homeListHttpHelper = new InfoListListHttpHelper(activity, Url.FIND_ALL_DEVICE_URL, FIND_BUILDING_WHAT, handler, true, url_params);
        homeListHttpHelper.setParseJsonData(new InfoListParseJsonData());
        homeListHttpHelper.getHttpRequest(true);
    }

    /**
     * 查找维保单位
     */
    private void initFindCompanyHttpHelper() {
        if (!Utils.hasNetwork(activity)){
            ToastUtils.showToast(activity, "网络未连接");
            return;
        }
        if (isUpdateImg){
            ToastUtils.showToast(activity, "正在上传图片");
            return;
        }
        include_progress_bar.setVisibility(View.VISIBLE);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id", UserInfoModel.getCompandId(activity));

        HashMap<String, String> url_params = new HashMap<>();
        url_params.put(ConstData.JSONOBJECT, JSONUtil.mapToJSonStr(hashMap));
        HttpHelper homeListHttpHelper = new InfoListListHttpHelper(activity, Url.FIND_WEIBAO_COMPANY_URL, FINA_COMPANY_WHAT, handler, true, url_params);
        homeListHttpHelper.setParseJsonData(new InfoListParseJsonData());
        homeListHttpHelper.getHttpRequest(true);
    }

    /**
     * 添加列表
     */
    private void initAddDetailHttpHelper() {
        if (!Utils.hasNetwork(activity)){
            ToastUtils.showToast(activity, "网络未连接");
            return;
        }
        if (isUpdateImg){
            ToastUtils.showToast(activity, "正在上传图片");
            return;
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        if (StringUtils.isEmpty(tv_select_building.getText().toString())){
            ToastUtils.showToast(activity, "未选择楼层");
            return;
        }else {
            FloorModel floorModel = buildingModels.get(currentSelectBuilding).getChildren().get(currentSelectFloor);
            hashMap.put("buildingId", floorModel.getBuildingId());
            hashMap.put("floorId", floorModel.getId());
        }
        // 选择设备
        if (StringUtils.isEmpty(et_device_name.getText().toString())){
            ToastUtils.showToast(activity, "未选择设备");
            return;
        }else {
            if (currentSelectDevice < 0){
                hashMap.put("equName", et_device_name.getText().toString());
            }else {
                DeviceModel deviceModel = buildingModels.get(currentSelectBuilding)
                        .getChildren().get(currentSelectFloor)
                        .getChildren().get(currentSelectDevice);
                hashMap.put("equId", deviceModel.getEquId());
                hashMap.put("equName", et_device_name.getText().toString());
            }
        }
        // 添加位置信息
        if (StringUtils.isEmpty(tv_weibao_company.getText().toString())){
            ToastUtils.showToast(activity, "未选择维保单位");
            return;
        }else {
            hashMap.put("repairCompanyId", weiBaoCompanyModels.get(currentSelectCompany).getId());
        }

        // 添加描述
        if (StringUtils.isEmpty(et_error_content.getText().toString())){
            ToastUtils.showToast(activity, "未输入描述");
            return;
        }else {
            hashMap.put("remark", et_error_content.getText().toString());
        }

        // 添加位置信息
        if (StringUtils.isEmpty(et_deice_location.getText().toString())){
            ToastUtils.showToast(activity, "未输入位置");
            return;
        }else {
            hashMap.put("address", et_deice_location.getText().toString());
        }

        // 添加图片信息
        boolean canCommit = false;
        for (int i = 0; i < 3; i++){
            if (!StringUtils.isEmpty(imgUrl[i])){
                canCommit = true;
                break;
            }
        }
        if (!canCommit){
            ToastUtils.showToast(activity, "未拍照");
            return;
        }
        StringBuilder imgStrBuilder = new StringBuilder();
        if (!StringUtils.isEmpty(imgUrl[0])) {
            imgStrBuilder.append(imgUrl[0]);
        }
        if (!StringUtils.isEmpty(imgUrl[1])) {
            imgStrBuilder.append("," + imgUrl[1]);
        }
        if (!StringUtils.isEmpty(imgUrl[2])) {
            imgStrBuilder.append("," + imgUrl[2]);
        }
        hashMap.put("imgs", imgStrBuilder.toString());
        UserInfoModel userInfoModel = UserInfoModel.getUserInfo(activity);
        hashMap.put("userId", userInfoModel.getId());
        hashMap.put("reporter",userInfoModel.getRealName());
        hashMap.put("reporterPhone", userInfoModel.getPhone());
        hashMap.put(ConstData.COMPANYID, UserInfoModel.getCompandId(activity));

        include_progress_bar.setVisibility(View.VISIBLE);
        HashMap<String, String> url_params = new HashMap<>();
        url_params.put(ConstData.JSONOBJECT, JSONUtil.mapToJSonStr(hashMap));
        HttpHelper homeListHttpHelper = new InfoListListHttpHelper(activity, Url.YUYUE_SAVE_URL, SUBMIT_DETAIL_WHAT, handler, true, url_params);
        homeListHttpHelper.setParseJsonData(new InfoListParseJsonData());
        homeListHttpHelper.getHttpRequest(true);
    }


    private class InfoListListHttpHelper extends HttpHelper{
        public InfoListListHttpHelper(BaseActivity activity, String url, int what, Handler handler, boolean isPost, HashMap<String, String> params) {
            super(activity, url, what, handler, isPost, params);
        }
    }

    protected class InfoListParseJsonData extends ParseJsonData {

        public InfoListParseJsonData() {
        }

        @Override
        public void analyzeResult(String dataStr, Bundle bundle, int what) {
            try {
                switch (what) {
                    case SUBMIT_DETAIL_WHAT:
                        break;
                    case FIND_BUILDING_WHAT:
                        LinkedList<BuildingModel> buildingModels =
                                (LinkedList<BuildingModel>) JSONUtil.jsonArrayToBeanList(new JSONArray(dataStr), BuildingModel.class);
                        bundle.putSerializable("list", buildingModels);
                        bundle.putBoolean(REQUEST_OK, true);
                        break;
                    case FINA_COMPANY_WHAT:
                        LinkedList<WeiBaoCompanyModel> weiBaoCompanyModels =
                                (LinkedList<WeiBaoCompanyModel>) JSONUtil.jsonArrayToBeanList(new JSONArray(dataStr), WeiBaoCompanyModel.class);
                        bundle.putSerializable("list", weiBaoCompanyModels);
                        bundle.putBoolean(REQUEST_OK, true);
                        break;
                }
            }catch (Exception e){
                bundle.putBoolean(REQUEST_OK, false);
                bundle.putString(ERROR_MSG, ANALYZE_ERROR_MSG);
            }
        }
    }

    @Override
    protected void dealWithMyMsgs(Message msg) {
        super.dealWithMyMsgs(msg);
        Bundle bundle = msg.getData();
        if ((!bundle.getBoolean(ParseJsonData.REQUEST_OK) ||
                !ParseJsonData.REQUEST_OK_CODE.equals(bundle.getString(ParseJsonData.ERROR_CODE)))){
            include_progress_bar.setVisibility(View.GONE);
            if (msg.what == SUBMIT_DETAIL_WHAT && ParseJsonData.REQUEST_OK_CODE.equals(bundle.getString(ParseJsonData.ERROR_CODE))){
                PreferencesUtils.putString(activity, ConstData.NEEDREFRESH, "0");
                isSubmitSuccess = true;
                activity.finish();
            }
            if (msg.what >= ConstData.COMMIT_IMG_WHAT){
                isUpdateImg = false;
            }
            return;
        }
        switch (msg.what){
            case SUBMIT_DETAIL_WHAT:
                include_progress_bar.setVisibility(View.GONE);
                isSubmitSuccess = true;
                activity.finish();
                break;
            case FIND_BUILDING_WHAT:
                include_progress_bar.setVisibility(View.GONE);
                buildingModels.clear();
                buildingModels = (List<BuildingModel>) bundle.getSerializable("list");
                if (null != buildingModels && buildingModels.size() > 0) {
                    List<BannerModel> bannerModels = new ArrayList<>();
                    for (BuildingModel buildingModel:buildingModels){
                        BannerModel bannerModel = new BannerModel();
                        bannerModel.setTitle(buildingModel.getName());
                        bannerModels.add(bannerModel);
                    }
                    chooseBuildingDialoag = DialogUtils.getChooseListDialog(activity, bannerModels, "选择楼栋", -1, new BaseRecyclerAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(View v, int position, Object object) {
                            DialogUtils.closeDialog(chooseBuildingDialoag);
                            currentSelectBuilding = position;
                            // 显示楼层列表
                            List<FloorModel> floorModels = buildingModels.get(position).getChildren();
                            if (null != floorModels && floorModels.size() > 0) {
                                List<BannerModel> bannerModels = new ArrayList<>();
                                for (FloorModel floorModel:floorModels){
                                    BannerModel bannerModel = new BannerModel();
                                    bannerModel.setTitle(floorModel.getName());
                                    bannerModels.add(bannerModel);
                                }
                                chooseFloorDialoag = DialogUtils.getChooseListDialog(activity, bannerModels, "选择楼层", -1, new BaseRecyclerAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(View v, int position, Object object) {
                                        DialogUtils.closeDialog(chooseFloorDialoag);
                                        currentSelectFloor = position;
                                        FloorModel currentFloorModel = buildingModels.get(currentSelectBuilding).getChildren().get(position);
                                        tv_select_building.setText(currentFloorModel.getBuildingName() + "(" + currentFloorModel.getName() + ")");
                                    }
                                });
                                chooseFloorDialoag.show();
                            }else {
                                ToastUtils.showToast(activity, "暂无楼层信息");
                                currentSelectBuilding = -1;
                            }
                        }
                    });
                    chooseBuildingDialoag.show();
                }else {
                    ToastUtils.showToast(activity, "暂无楼栋信息");
                }
                break;
            case FINA_COMPANY_WHAT:
                include_progress_bar.setVisibility(View.GONE);
                weiBaoCompanyModels = (List<WeiBaoCompanyModel>) bundle.getSerializable("list");
                if (null != weiBaoCompanyModels && weiBaoCompanyModels.size() > 0){
                    List<BannerModel> bannerModels = new ArrayList<>();
                    for (WeiBaoCompanyModel model : weiBaoCompanyModels) {
                        BannerModel bannerModel = new BannerModel();
                        bannerModel.setTitle(model.getName());
                        bannerModels.add(bannerModel);
                    }
                    chooseCompanyDialog = DialogUtils.getChooseListDialog(activity, bannerModels,
                            "选择维保单位", currentSelectCompany,
                            new BaseRecyclerAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(View v, int position, Object data) {
                                    currentSelectCompany = position;
                                    tv_weibao_company.setText(((BannerModel)data).getTitle());
                                    chooseCompanyDialog.dismiss();
                                }
                            });
                    chooseCompanyDialog.show();
                }else {
                    ToastUtils.showToast(activity, "暂无维保单位信息");
                }
                break;
        }
        if (msg.what >= ConstData.COMMIT_IMG_WHAT){
            isUpdateImg = false;
            include_progress_bar.setVisibility(View.GONE);
            switch (msg.what - ConstData.COMMIT_IMG_WHAT){
                case ConstData.PHOTO_DEVICE_LIST_1:
                    imgUrl[0] = bundle.getString("fileName");
                    break;
                case ConstData.PHOTO_DEVICE_LIST_2:
                    imgUrl[1] = bundle.getString("fileName");
                    break;
                case ConstData.PHOTO_DEVICE_LIST_3:
                    imgUrl[2] = bundle.getString("fileName");
                    break;
            }
            updateImageView();
        }
    }

    /**
     * 跳转拍照和相册
     * @param photoWhat
     * @param isPlayPhoto
     */
    public void addImageOrPlayPhoto(int photoWhat, boolean isPlayPhoto){
        if (activity instanceof ShowFragActivity) {
            if (isPlayPhoto) {
                ((ShowFragActivity)activity).skipToPlayPhoto(photoWhat);
            } else {
                ((ShowFragActivity)activity).initSubmitPicView(photoWhat);
            }
        }
    }

    @Override
    public void imgSuccessAction(File file, String img, int photoWhat) {
        super.imgSuccessAction(file, img, photoWhat);
        commitPic(file, photoWhat, img);
    }

    /**
     * 提交图片
     * @param file
     * @param what
     */
    private void commitPic(final File file, final int what, final String img){
        isUpdateImg = true;
        include_progress_bar.setVisibility(View.VISIBLE);
        MinioUtils.upLoadFile(activity, what, file, handler);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!isSubmitSuccess){
            for (int i = 0; i < imgUrl.length; i++){
                if (!StringUtils.isEmpty(imgUrl[i])){
                    MinioUtils.removeFile(activity, imgUrl[i], handler);
                }
            }
        }
        DialogUtils.closeDialog(chooseBuildingDialoag);
        DialogUtils.closeDialog(chooseFloorDialoag);
        DialogUtils.closeDialog(chooseDeviceDialoag);
        DialogUtils.closeDialog(chooseCompanyDialog);
    }

    private void updateImageView(){
        updateImageUrl();
        int size = 0;
        for (int i = 0; i < imgUrl.length; i++){
            if (!StringUtils.isEmpty(imgUrl[i])){
                size++;
            }
        }
        switch (size){
            case 3:
                rl_device_photo_1.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_1, imgUrl[0]);
                iv_photo_delete_1.setVisibility(View.VISIBLE);

                rl_device_photo_2.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_2, imgUrl[1]);
                iv_photo_delete_2.setVisibility(View.VISIBLE);

                rl_device_photo_3.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_3, imgUrl[2]);
                iv_photo_delete_3.setVisibility(View.VISIBLE);
                break;
            case 2:
                rl_device_photo_1.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_1, imgUrl[0]);
                iv_photo_delete_1.setVisibility(View.VISIBLE);

                rl_device_photo_2.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_2, imgUrl[1]);
                iv_photo_delete_2.setVisibility(View.VISIBLE);

                rl_device_photo_3.setVisibility(View.VISIBLE);
                iv_device_photo_3.setImageResource(R.mipmap.icon_duty_detail_photo_big);
                iv_photo_delete_3.setVisibility(View.INVISIBLE);
                break;
            case 1:
                rl_device_photo_1.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_1, imgUrl[0]);
                iv_photo_delete_1.setVisibility(View.VISIBLE);

                rl_device_photo_2.setVisibility(View.VISIBLE);
                iv_device_photo_2.setImageResource(R.mipmap.icon_duty_detail_photo_big);
                iv_photo_delete_2.setVisibility(View.INVISIBLE);

                rl_device_photo_3.setVisibility(View.INVISIBLE);
                break;
            case 0:
                rl_device_photo_1.setVisibility(View.VISIBLE);
                iv_device_photo_1.setImageResource(R.mipmap.icon_duty_detail_photo_big);
                iv_photo_delete_1.setVisibility(View.INVISIBLE);

                rl_device_photo_2.setVisibility(View.INVISIBLE);

                rl_device_photo_3.setVisibility(View.INVISIBLE);
                break;
        }
    }

    /**
     * 调整图片链接顺序
     */
    private void updateImageUrl(){
        if (null != imgUrl && imgUrl.length > 0){
            for (int i = 0; i < imgUrl.length; i++){
                if (StringUtils.isEmpty(imgUrl[i])){
                    for (int j = i + 1; j < imgUrl.length; j++){
                        if (!StringUtils.isEmpty(imgUrl[j])){
                            imgUrl[i] = imgUrl[j];
                            imgUrl[j] = "";
                            break;
                        }
                    }
                }
            }
        }
    }
}
